{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 1: Make your own\n",
    "\n",
    "Dictionaries can be nested, which means that dictionary keys can contain dictionaries themselves.\n",
    "\n",
    "1. Create two dictionaries, each representing one of your favorite musical artist. Each dictionary should have the following keys / value-type: `name`: (string) , `genre`: (string), `songs` (list), `age`: (integer)\n",
    "\n",
    "2. Create an outer dictionary called `musical_artists` that contain the two inner dictionaries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'musical_artists': [{'name': 'Journey', 'genre': 'Classic Rock', 'songs': [\"Don't Stop Believin'\", 'Wheel in the Sky'], 'age': 34}, {'name': 'Justin Bieber', 'genre': 'Pop', 'songs': ['Sorry', 'Baby'], 'age': 22}]}\n"
     ]
    }
   ],
   "source": [
    "journey = {\"name\": \"Journey\", \"genre\": \"Classic Rock\", \"songs\": [\"Don't Stop Believin'\", \"Wheel in the Sky\"], \"age\": 34}\n",
    "bieber = {\"name\": \"Justin Bieber\", \"genre\": \"Pop\", \"songs\": [\"Sorry\", \"Baby\"], \"age\": 22}\n",
    "\n",
    "musical_artists = {\"musical_artists\": [journey, bieber]}\n",
    "\n",
    "print(musical_artists)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Journey': {'name': 'Journey', 'genre': 'Classic Rock', 'songs': [\"Don't Stop Believin'\", 'Wheel in the Sky'], 'age': 34}, 'Justin Bieber': {'name': 'Justin Bieber', 'genre': 'Pop', 'songs': ['Sorry', 'Baby'], 'age': 22}}\n"
     ]
    }
   ],
   "source": [
    "musical_artists = {\"Journey\": journey, \"Justin Bieber\": bieber}\n",
    "\n",
    "print(musical_artists)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 2: Compute the cost\n",
    "\n",
    "Using the dictionary below and a for loop, to calculate how much it'll cost you to buy 2 pieces of each fruit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'apples': 0.49, 'oranges': 0.99, 'pears': 1.49, 'bananas': 0.32}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.579999999999999\n"
     ]
    }
   ],
   "source": [
    "total_cost = 0\n",
    "for fruit in d.keys():\n",
    "    total_cost += (d[fruit] * 2)\n",
    "    \n",
    "print(total_cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 3: Check to see a key\n",
    "\n",
    "To see if something is in a container, use the `in` operator. This works for both lists and dictionaries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "l = [\"Afghanistan\", \"Canada\", \"Sierra Leone\", \"Denmark\", \"Japan\"]\n",
    "d = {'apples': 0.49, 'oranges': 0.99, 'pears': 1.49, 'bananas': 0.32}\n",
    "\n",
    "print('Canada' in l)\n",
    "print('grapefruit' in d)\n",
    "print('grapefruit' not in d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below, I've given you a **list** containing 5 **dictionaries** representing some American states. \n",
    "\n",
    "1. Loop through all the dictionaries in the list\n",
    "2. Check to see if \"state bird\" is in the dictionary\n",
    "3. If the key is NOT in the dictionary, add the key and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it to the value \"unknown\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "states = [{'state': 'Ohio', 'population': 11.6, 'year in union': 1803, 'state bird': 'Northern cardinal', 'capital': 'Columbus'},\n",
    "          {'state': 'Michigan', 'population': 9.9, 'year in union': 1837, 'capital': 'Lansing'},\n",
    "          {'state': 'California', 'population': 39.1, 'year in union': 1850, 'state bird': 'California quail', 'capital': 'Sacramento'},\n",
    "          {'state': 'Florida', 'population': 20.2, 'year in union': 1834, 'capital': 'Tallahassee'},\n",
    "          {'state': 'Alabama', 'population': 4.9, 'year in union': 1819, 'capital': 'Montgomery'}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'state': 'Ohio', 'population': 11.6, 'year in union': 1803, 'state bird': 'Northern cardinal', 'capital': 'Columbus'}, {'state': 'Michigan', 'population': 9.9, 'year in union': 1837, 'capital': 'Lansing', 'state bird': 'unknown'}, {'state': 'California', 'population': 39.1, 'year in union': 1850, 'state bird': 'California quail', 'capital': 'Sacramento'}, {'state': 'Florida', 'population': 20.2, 'year in union': 1834, 'capital': 'Tallahassee', 'state bird': 'unknown'}, {'state': 'Alabama', 'population': 4.9, 'year in union': 1819, 'capital': 'Montgomery', 'state bird': 'unknown'}]\n"
     ]
    }
   ],
   "source": [
    "for dic in states:\n",
    "    if \"state bird\" not in dic:\n",
    "        dic[\"state bird\"] = \"unknown\"\n",
    "        \n",
    "print(states)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
